/**
	Author : behrooz
	Date : Nov 12, 2011
	Project : Curve Descriptor
 **/
package edu.oregonstate.vision;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import edu.oregonstate.vision.common.Point;

public class TrackReader {
	public List<Track> parseTracks(String fileName) {
		List<Track> tracks = new ArrayList<Track>();
		try {
			BufferedReader bufferedReader = new BufferedReader(new FileReader(
					new File(fileName)));
			String string = null;
			do {
				string = bufferedReader.readLine();
				if (string == null)
					break;
				string = string.replace(" ", "");
				String[] strings = string.split("-");
				String[] pointsXYs = strings[1].split("[(,;)]");
				List<Point> points = new ArrayList<Point>();
				int index = 0;
				Point point = null;
				for (String stringXY : pointsXYs) {
					if (stringXY.trim().length() == 0)
						continue;
					if (index % 2 == 0) {
						point = new Point(Float.valueOf(stringXY), 0.0f);
					} else {
						point.setY(Float.valueOf(stringXY));
						point.setZ(index / 2 * 0.05f);
						points.add(point);
					}
					index++;
				}
				strings = strings[0].split(":");
				String temp = strings[1].substring(1, strings[1].length() - 1);
				Track track = new Track(strings[0], Integer.valueOf(temp
						.split(",")[0]), Integer.valueOf(temp.split(",")[1]));
				track.setPoints(points);
				tracks.add(track);
			} while (string != null);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tracks;
	}
}
